package com.jh.paymentchannel.pojo;

import com.google.common.base.MoreObjects;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Objects;


/**
 * 用户交易额统计 DTO.
 *
 * @author Fuchun
 * @since 1.0
 */
public class UserTurnover implements Serializable {

    private final Long userId;
    /** 成交笔数。*/
    private final Integer dealNumber;
    /** 交易总额。*/
    private final BigDecimal totalAmount;

    private final BigDecimal totalProfit;

    public UserTurnover(Long userId, Integer dealNumber, BigDecimal totalAmount) {
        this(userId, dealNumber, totalAmount, null);
    }

    public UserTurnover(Long userId, Integer dealNumber, BigDecimal totalAmount, BigDecimal totalProfit) {
        this.userId = userId;
        this.dealNumber = dealNumber;
        this.totalAmount = totalAmount;
        this.totalProfit = totalProfit;
    }

    public Long getUserId() {
        return userId;
    }

    public Integer getDealNumber() {
        return dealNumber;
    }

    public BigDecimal getTotalAmount() {
        return totalAmount;
    }

    public BigDecimal getTotalProfit() {
        return totalProfit;
    }

    public UserTurnover add(BigDecimal amount, BigDecimal profit) {
        return new UserTurnover(userId,
                dealNumber + 1,
                totalAmount.add(amount),
                totalProfit == null ? profit : totalProfit.add(profit));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserTurnover turnover = (UserTurnover) o;
        return Objects.equals(userId, turnover.userId) &&
                Objects.equals(dealNumber, turnover.dealNumber) &&
                Objects.equals(totalAmount, turnover.totalAmount) &&
                Objects.equals(totalProfit, turnover.totalProfit);
    }

    @Override
    public int hashCode() {
        return Objects.hash(userId, dealNumber, totalAmount, totalProfit);
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("userId", userId)
                .add("dealNumber", dealNumber)
                .add("totalAmount", totalAmount)
                .add("totalProfit", totalProfit)
                .toString();
    }
}
